package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.core.Promise

////////////////////
// i18n
////////////////////
/**
 * Use the `Browser.i18n` infrastructure to implement internationalization across your whole app or extension.
 *
 * Manifest: "default_locale"
 */
interface I18n {
    // Detects the language of the provided text using CLD.
    @ChromeMinVersion(47)
    @PromiseStyleMinVersion(99)
    fun detectLanguage(
        text: String,
        callback: Value1Function<LanguageDetectionResult>? = null
    ): Promise<LanguageDetectionResult>

    // Gets the accept-languages of the browser.
    // This is different from the locale used by the browser;
    // to get the locale, use i18n.getUILanguage.
    @PromiseStyleMinVersion(99)
    fun getAcceptLanguages(
        callback: Value1Function<List<LanguageCode>>? = null
    ): Promise<List<LanguageCode>>

    // Gets the localized string for the specified message.
    // If the message is missing, this method returns an empty string ('').
    // If the format of the getMessage() call is wrong — for example,
    // messageName is not a string or the substitutions array has more than 9 elements — this method returns undefined.
    // @return Message localized for current locale.
    fun getMessage(
        // The name of the message, as specified in the messages.json file.
        messageName: String,
        // Up to 9 substitution strings, if the message requires any.
        substitutions: Any? = null,
        options: GetMessageOptions? = null
    ): String

    // Gets the browser UI language of the browser.
    // This is different from i18n.getAcceptLanguages which returns the preferred user languages.
    // @return The browser UI language code such as en-US or fr-FR.
    fun getUILanguage(): String

    // An ISO language code such as en or fr.
    // For a complete list of languages supported by this method, see kLanguageInfoTable.
    // For an unknown language, und will be returned, which means that [percentage] of the text is unknown to CLD
    @ChromeMinVersion(47)
    data class LanguageCode(
        var lan: String
    )

    // LanguageDetectionResult object that holds detected langugae reliability and array of DetectedLanguage
    data class LanguageDetectionResult(
        // CLD detected language reliability
        var isReliable: Boolean,
        var language: List<LanguageDetectionResultItem>
    )

    data class LanguageDetectionResultItem(
        var language: String,
        // The percentage of the detected language
        var percentage: Number,
    )

    @ChromeMinVersion(79)
    data class GetMessageOptions(
        // Escape < in translation to &lt;. This applies only to the message itself, not to the placeholders.
        // Developers might want to use this if the translation is used in an HTML context.
        // Closure Templates used with Closure Compiler generate this automatically.
        var escapeLt: Boolean?
    )
}